/*1942 game clone
 * Authors:
 * Abdollah Tabareh
 * Bharat Bhushan Konka
 * Martin Svensson 
 * Richard Zianne
 * Tigran Harutyunyan
 * 
 * 2010
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Game1942
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class PlayerPlane : EnemyPlane
    {

        protected const short WIDTH = 65;
        protected const short HEIGHT = 65;
        private int _countDown = 4;
        private int _prop = 0;
        private bool _isReceivingDamage = true;
        private int _countDownAfterReset = 150;
        public PlayerPlane(Game game, ref Texture2D texture)
            : base(game)
        {
            _power = 50;
            _planeSpeed = 1;
            _position = new Vector2(Util.ScreenWidth / 2, Util.ScreenHeight / 2);
            _texture = texture;
            _planeWith = 60;
            _planeHight = 44;

            _isDying = false;
            _isDead = false;
            

            //_spriteRectangle = new Rectangle(4, 70, _planeWith, _planeHight);
            _spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            Frames = new List<Rectangle>();
            Rectangle frame = new Rectangle();
            frame.X = 6;
            frame.Y = 412;
            frame.Width = _planeWith;
            frame.Height = _planeHight;
            Frames.Add(frame);//index 0 

            frame.X = 138;
            Frames.Add(frame);//index 1 
            frame.X = 72;
            Frames.Add(frame);//index 2
            frame.X = 138;
            Frames.Add(frame);//index 3 

            //Explosion
            frame.X = 4;
            frame.Y = 301;
            frame.Width = WIDTH;
            frame.Height = HEIGHT;
            Frames.Add(frame);//index 4 
            frame.X = 70;
            Frames.Add(frame);//index 5
            frame.X = 136;
            Frames.Add(frame);//index 6
            frame.X = 202;
            Frames.Add(frame);//index 7
            frame.X = 268;
            Frames.Add(frame);//index 8
            frame.X = 334;
            Frames.Add(frame);//index 9
            frame.X = 400;
            Frames.Add(frame);//index 10
            frame.X = 204;
            frame.Y = 412;
            frame.Width = _planeWith;
            frame.Height = _planeHight;
            Frames.Add(frame);//index 11



            _spriteRectangle = Frames.ElementAt(0); 
            _position = new Vector2(300, 300);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (!_isReceivingDamage)
            {
                _spriteRectangle = Frames.ElementAt(11);                
            } 
            else
            if (_prop == 0)
            {
                _spriteRectangle = Frames.ElementAt(0);
                _prop++;
            }
            else if (_prop == 1)
            {
                _spriteRectangle = Frames.ElementAt(1);
                _prop++;
            }
            else if (_prop == 2)
            {
                _spriteRectangle = Frames.ElementAt(2);
                _prop++;
            }
            else if (_prop == 3)
            {
                _spriteRectangle = Frames.ElementAt(3);
                _prop =0;
            }

            

            if (_isDying)
            {
                if (_countDown >= Frames.Count)
                {
                    _isDead = true;
                    putInPosition(-65, -65);
                    _countDown = 4;


                }
                else
                {
                    _spriteRectangle = Frames.ElementAt(_countDown);
                    _countDown++;
                }

            }


            KeyboardState keyborard = Keyboard.GetState();
            if (keyborard.IsKeyDown(Keys.Down))
            {
                if (_position.Y < Util.ScreenHeight - _planeHight)
                {
                    _position.Y += 2;
                }
            }
            if (keyborard.IsKeyDown(Keys.Up))
            {
                if (!(_position.Y == 0))
                {
                    _position.Y -= 2;
                }
            }
            if (keyborard.IsKeyDown(Keys.Left))
            {
                if (!(_position.X == 0))
                {
                    _position.X -= 2;
                }
                

            }
            if (keyborard.IsKeyDown(Keys.Right))
            {
                if (_position.X < Util.ScreenWidth - _planeWith)
                {
                    _position.X += 2;
                }
                
            }


            //Self destrukt test
            if (keyborard.IsKeyDown(Keys.D))
            {
                _isDying = true;

            }

            if (!_isReceivingDamage)
            {
                if (_countDownAfterReset > 0)
                {
                    _countDownAfterReset--;
                }
                else
                {
                    _countDownAfterReset = 150;
                    _isReceivingDamage = true;
                }
            }

            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            _spriteBatch.Draw(_texture, _position, _spriteRectangle, Color.White);
            _spriteBatch.End();
            base.Draw(gameTime);
        }
        public float getPositionX()
        {
            return _position.X;
        }
        public float getPositionY()
        {
            return _position.Y;
        }

        public override void hit(int power)
        {
            if (!_isReceivingDamage)
            {
                return;
            }
            base.hit(10);
        }
        public void SetReceivingDamageFalse()
        {
            _isReceivingDamage = false;
        }
        
    }
}